//
//  TWWaterLayout.m
//  自定义布局
//
//  Created by 耐克了解了 on 17/4/17.
//  Copyright © 2017年 耐克了解了. All rights reserved.
//

#import "TWWaterLayout.h"
#import "XMGShop.h"

static const CGFloat TWDefultColumMargin = 10;
static const CGFloat TWDefultRowMargin = 10;
static const UIEdgeInsets TWDefultEdgsInsets = {10, 10, 10, 10};
static const CGFloat TWDefultsColumCount = 3;

@interface TWWaterLayout ()

@property (nonatomic,strong) NSMutableArray *attrsArray;

@property (nonatomic,strong) NSMutableArray *maxHeights;


- (CGFloat)rowMargin;
- (CGFloat)columnMargin;
- (NSInteger)columnCount;
- (UIEdgeInsets)edgeInsets;

@end

@implementation TWWaterLayout

- (CGFloat)rowMargin{
    if ([self.delegate respondsToSelector:@selector(rowMarginInWaterFlowlayout:)]) {
        return [self.delegate rowMarginInWaterFlowlayout:self];
    }else{
        return TWDefultRowMargin;
    }
}

- (CGFloat)columnMargin
{
    if ([self.delegate respondsToSelector:@selector(columnMarginInWaterFlowlayout:)]) {
        return [self.delegate columnMarginInWaterFlowlayout:self];
    }else{
        return TWDefultColumMargin;
    }
}

- (NSInteger)columnCount
{
    if ([self.delegate respondsToSelector:@selector(columnCountInWaterFlowlayout:)]) {
        return [self.delegate columnCountInWaterFlowlayout:self];
    }else{
        return TWDefultsColumCount;
    }
}

- (UIEdgeInsets)edgeInsets
{
    if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterFlowlayout:)]) {
        return [self.delegate edgeInsetsInWaterFlowlayout:self];
    }else{
        return TWDefultEdgsInsets;
    }
}

- (NSMutableArray *)maxHeights
{
    if (!_maxHeights) {
        _maxHeights = [NSMutableArray array];
    }
    return _maxHeights;
}

- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

//初始化
- (void)prepareLayout
{
    [super prepareLayout];
    [self.maxHeights removeAllObjects];
    for (NSInteger i = 0; i < self.columnCount; i++) {
        [self.maxHeights addObject:@(self.edgeInsets.top)];
    }
    
    //清除之前的布局，以防数组越来越大
    [self.attrsArray removeAllObjects];
    
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    
    for (NSInteger i = 0; i < count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        UICollectionViewLayoutAttributes *atts = [self layoutAttributesForItemAtIndexPath:indexPath];
   
        [self.attrsArray addObject:atts];
    }
}

//决定cell的排布
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
   
    
    return  self.attrsArray;
}


- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attrs =[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    //设置layout属性
    CGFloat collectionViewWidth = self.collectionView.frame.size.width;
    
    CGFloat w = (collectionViewWidth - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1)*self.columnMargin) /self.columnCount ;
    CGFloat h = [self.delegate waterFlowLayout:self heighForItemIndex:indexPath.item itemWidth:w];
    
    //找出高度最短的那一列
//    __block NSInteger destColum = 0;
//    __block CGFloat minColumHeight = 0;
//    [self.maxHeights enumerateObjectsUsingBlock:^(NSNumber  * columHeightNumber, NSUInteger idx, BOOL * _Nonnull stop) {
//       CGFloat  columHeight = columHeightNumber.doubleValue;
//        if (minColumHeight > columHeight) {
//            minColumHeight = columHeight;
//            destColum = idx;
//        }
//        
//    }];
         NSInteger destColum = 0;
         CGFloat minColumHeight = [self.maxHeights.firstObject doubleValue];;
    for (NSInteger i = 1; i < self.columnCount; i++) {
       CGFloat columHeight = [self.maxHeights[i]doubleValue];
        if (minColumHeight > columHeight) {
            minColumHeight = columHeight;
            destColum = i;
        }
    }
    
    CGFloat x = self.edgeInsets.left + destColum *(w + self.columnMargin);
      CGFloat y =  minColumHeight;
    if (y != self.edgeInsets.top) {
        y += self.rowMargin;
    }
  
    attrs.frame = CGRectMake(x, y, w, h);
    
    //更新布局
    self.maxHeights[destColum] = @(CGRectGetMaxY(attrs.frame));
    
    return  attrs;
}

- (CGSize)collectionViewContentSize
{
    CGFloat maxColumHeight = [self.maxHeights.firstObject doubleValue];;
    for (NSInteger i = 1; i < self.columnCount; i++) {
        CGFloat columHeight = [self.maxHeights[i]doubleValue];
        if (maxColumHeight < columHeight) {
            maxColumHeight = columHeight;
            
        }
    }

    
    return CGSizeMake(0, maxColumHeight + self.edgeInsets.bottom);
}


@end
